﻿
Partial Public Class projekt_erstellen_drei
    Inherits System.Web.UI.Page
    Private _kunde As String
    Private _ansprechpartner As String



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim db As hc_databaseDataContext = New hc_databaseDataContext
        _kunde = Request.QueryString("Kunde")
        _ansprechpartner = Request.QueryString("Ansprechpartner")
        If Not IsPostBack Then
            Dim schemaarray As New ArrayList
            With schemaarray
                .Add("Prozesse")
                .Add("Studien")
                .Add("Strategie")
                .Add("Personal")
                .Add("Marketing")
                .Add("Gründung")
            End With
            dd_schema.DataSource = schemaarray
            dd_schema.DataBind()

            Dim var2 = From it In db.Mitgliede_Anwaerter _
                                Distinct Select Bla = (it.Vorname + " " + it.Nachname) Order By Bla

            For Each s As String In var2
                dd_pm.Items.Add(s)
                dd_jpm.Items.Add(s)

            Next
            dd_jpm.Items.Add("Noch nicht bekannt")
            dd_pm.Items.Add("Noch nicht bekannt")
            dd_jpm.SelectedValue = "Noch nicht bekannt"
            dd_pm.SelectedValue = "Noch nicht bekannt"
        End If

    End Sub

    Protected Sub btn_fertig_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_fertig.Click

        Dim db As hc_databaseDataContext = New hc_databaseDataContext
        Try
            Dim var = (From it In db.Projekts _
                      Where it.Name = txt_projektname.Text _
                      Select it).Single
            lbl_fehler.Text = "Es existiert bereits ein Projekt mit diesem Namen"
        Catch ex As Exception




            Dim projekt1 = New Projekt
            projekt1.Name = txt_projektname.Text
            projekt1.Startdatum_erstkontakt = Convert.ToDateTime(txt_erstkontakt.Text)
            projekt1.Beschreibung = txt_beschreibung.Text
            projekt1.status = dd_status.SelectedValue
            projekt1.Projektschema = dd_schema.SelectedValue

            projekt1.Herkunft = txt_entstanden.Text

            Dim var = (From it In db.Kundes _
                      Where it.ID = Integer.Parse(_kunde) _
                      Select it).Single
            projekt1.Kundes = var

            If Session("Intern") = 1 Then
                projekt1.Projekt_extern_intern = 1
            Else
                projekt1.Projekt_extern_intern = 0
            End If
            Dim funktionen As New stored_functions
            Dim pmvorname As String = ""
            Dim pmnachname As String = ""
            Dim jpmvorname As String = ""
            Dim jpmnachname As String = ""
            If Not dd_pm.SelectedValue = "Noch nicht bekannt" Then
                pmvorname = funktionen.stringInVorundNachnameZerlegen(dd_pm.SelectedItem.Text)(0)
                pmnachname = funktionen.stringInVorundNachnameZerlegen(dd_pm.SelectedItem.Text)(1)
            End If
            If Not dd_jpm.SelectedValue = "Noch nicht bekannt" Then
                jpmvorname = funktionen.stringInVorundNachnameZerlegen(dd_jpm.SelectedItem.Text)(0)
                jpmnachname = funktionen.stringInVorundNachnameZerlegen(dd_jpm.SelectedItem.Text)(1)

            End If





            projekt1.AnsprechpartnerID = Integer.Parse(_ansprechpartner)
            db.Projekts.InsertOnSubmit(projekt1)

            db.SubmitChanges()
            Dim phase5 As New Phase
            phase5.Bezeichnung = "Projektsteuerung"
            phase5.Projekt_ID = projekt1.ID
            db.Phase.InsertOnSubmit(phase5)
            db.SubmitChanges()
            Dim report5 As New Report
            report5.Mitgliede_AnwaerterID = 99999
            report5.Phase = phase5
            report5.Datum = Date.Now
            report5.Angenommen_pl = 1
            db.Reports.InsertOnSubmit(report5)
            db.SubmitChanges()
            If Not dd_jpm.SelectedValue = "Noch nicht bekannt" Then
                Try
                    Dim pm = (From it In db.Mitgliede_Anwaerter _
                              Where it.Vorname = pmvorname And it.Nachname = pmnachname _
                              Select it).Single
                    Dim arbeitan1 As New Arbeitet_an
                    With arbeitan1
                        .Projekt = projekt1
                        .Mitgliede_Anwaerter = pm
                        .rolle = "PM"
                    End With
                    db.Arbeitet_ans.InsertOnSubmit(arbeitan1)
                    db.SubmitChanges()
                Catch
                End Try

            End If
            If Not dd_jpm.SelectedValue = "Noch nicht bekannt" Then


                Try
                    Dim jpm = (From it In db.Mitgliede_Anwaerter _
                              Where it.Vorname = jpmvorname And it.Nachname = jpmnachname _
                              Select it).Single
                    Dim arbeitan2 As New Arbeitet_an
                    With arbeitan2
                        .Projekt = projekt1
                        .Mitgliede_Anwaerter = jpm
                        .rolle = "JPM"
                    End With
                    db.Arbeitet_ans.InsertOnSubmit(arbeitan2)
                    db.SubmitChanges()

                Catch
                End Try
            End If
            If Session("Intern") = 0 Then
                Response.Redirect("projekt_uebersicht_extern.aspx")
            Else
                Response.Redirect("projekt_uebersicht_intern.aspx")

            End If
        End Try

    End Sub

    

    Protected Sub txt_erstkontakt_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txt_erstkontakt.TextChanged

    End Sub

    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd_jpm.SelectedIndexChanged

    End Sub

    Protected Sub dd_schema_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd_schema.SelectedIndexChanged

    End Sub
End Class